<template>
  <div class="rent-out">
    <el-table :data="tableData"
              border
              :fit="true"
              style="width: 100%">
      <el-table-column label="房屋id"
                       prop="houseId" width="120">
      </el-table-column>
      <el-table-column label="地址"
                       prop="address" width="280">
      </el-table-column>
      <el-table-column label="状态" v-if="this.role==='renter'"
                       prop="status">
        <template slot-scope="scope">
           <el-tag>申请中</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" v-if="this.role==='admin'"
                       prop="status">
        <template slot-scope="scope">
           <el-tag>已同意</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="申请人"
                       prop="applyName">
      </el-table-column>
      <el-table-column label="申请人身份证号码"
                       prop="applyNumber">
      </el-table-column>
      <el-table-column label="申请人电话号码"
                       prop="phone">
      </el-table-column>

      <el-table-column label="操作" >
        <template slot-scope="scope">
          <el-button size="mini"
                     type="danger"
                     @click="handleDelete(scope.$index, scope.row)">撤销申请</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-box">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="pagination.currentPage"
        :page-size="pagination.pageSize"
        layout="total, prev, pager, next"
        :total="pagination.total">
      </el-pagination>
    </div>
  </div>
</template>
<script lang="js">
import axios from "axios";

export default {
  name: 'Rentoutlist',
  data(){
    return {
      tableData: [],
        showEditCon: false,
        showTable: true,
        role: '',
      pagination: {//分页相关模型数据
        currentPage: 1,//当前页码
        pageSize: 10,//每页显示的记录数
        total: 0,//总记录数
        type:"",
        name:"",
        description:"",
      }
    }
  },
  created(){
    this.role = this.$store.state.userInfo.role;
    this.getAll();
  },
  methods: {
    getAll: function (){
      axios.get("/apply/lease/"+this.pagination.currentPage).then((res)=>{
        if (res.data.code === 1){
          this.tableData = res.data.data.records
          this.pagination.currentPage = res.data.data.current
          this.pagination.total = res.data.data.total
          this.pagination.pageSize = res.data.data.size
        }
      })
    },
      handleDelete(index, row) {
          this.$confirm('确定要撤销申请吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        }).then(() => {
          // 访问接口
          axios.delete("/apply/"+row.houseId).then((res)=>{
            if (res.data.code === 1){
              this.$message.success(res.data.data)
            }else {
              this.$message.error(res.data.msg)
            }
          }).finally(this.getAll)
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消成功'
          });
        }).finally(this.getAll)
      },
    //切换页码
    handleCurrentChange(currentPage) {
      this.pagination.currentPage = currentPage;
      this.getAll();
    },
    }
}
</script>
<style lang="scss" scoped>
</style>
